using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using UnityEngine;

namespace TheWorldOfKongfuEasyMod;

[HarmonyPatch(typeof(PackageController))]
[HarmonyPatch("OnButtonClick")]
public static class Patch_PackageController_OnButtonClick
{
    public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
    {
        var codeMatcher = new CodeMatcher(instructions, generator);
        var codeMatcher1 = new CodeMatcher(instructions, generator);


        codeMatcher.MatchForward(false, new CodeMatch(OpCodes.Call, typeof(PackageController).GetMethod("UpdatePackageItemDetail", BindingFlags.Instance | BindingFlags.NonPublic))
        );
        if (!codeMatcher.IsValid)
        {
            throw new InvalidOperationException("Can't find PackageController.UpdatePackageItemDetail");
        }

        var pos1 = codeMatcher.Pos;
        Main.Logger.LogInfo(pos1);
        codeMatcher1.MatchForward(false, new CodeMatch(OpCodes.Call, typeof(MonoBehaviour).GetMethod("StartCoroutine",
            BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(IEnumerator) }, null))
        );
        if (!codeMatcher1.IsValid)
        {
            throw new InvalidOperationException("Can't find PackageController.UpdatePackageItemDetail");
        }

        var pos2 = codeMatcher1.Pos;
        Main.Logger.LogInfo(pos2);
        codeMatcher.RemoveInstructionsInRange(pos1 + 1, pos2 + 1);

        codeMatcher.InsertAndAdvance(
            new CodeInstruction(OpCodes.Ldarg_0),
            new CodeInstruction(OpCodes.Ldarg_0),
            new CodeInstruction(OpCodes.Ldfld, typeof(PackageController).GetField("packagerFunction", BindingFlags.Instance | BindingFlags.Public)),
            new CodeInstruction(OpCodes.Ldarg_1),
            new CodeInstruction(OpCodes.Call, typeof(Patch_PackageController_OnButtonClick).GetMethod("Do", BindingFlags.Static | BindingFlags.NonPublic))
        );
        return codeMatcher.Instructions();
    }

    static void Do(PackageController packageController, PackagerFunction packagerFunction, GameObject SelectedBtn)
    {
        if (packagerFunction == PackagerFunction.Shop || packagerFunction == PackagerFunction.Redeem || packagerFunction == PackagerFunction.Loan)
        {
            packageController.StartCoroutine(CommonFunc.DelayedOpenHoverOperation(SelectedBtn, false, true));
        }
        if(!Main.Setting._004isAutoClickDetail.Value)
            return;

        if (packagerFunction == PackagerFunction.Equip)
        {
            packageController.StartCoroutine(CommonFunc.DelayedOpenHoverOperation(SelectedBtn, false, true));
        }
        else if (packagerFunction == PackagerFunction.PlayerPackage || packagerFunction == PackagerFunction.Wugong)
        {
            var strings = SelectedBtn.name.Split('|');
            if (strings[0] != "0" && strings.Length > 1)
            {
                var row = CommonResourcesData.b07.Find_ID(strings[1]);
                if (row is { Style: "101" or "201" or "202" or "203" })
                {
                    packageController.StartCoroutine(CommonFunc.DelayedOpenHoverOperation(SelectedBtn, false, true));
                }
            }
        }
    }
}